package com.dzsw.trans.config;

import com.alibaba.druid.support.jakarta.StatViewServlet;
import com.alibaba.druid.support.jakarta.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 〈druid配置文件 启动监控〉
 */
//@Configuration
public class DruidConfig {

    /**
     * 配置Druid的监控
     * 1、配置一个管理后台的Servlet
     *
     * @return
     */
    @Bean
    public ServletRegistrationBean<StatViewServlet> statViewServlet() {
        //创建管理后台 设置访问前缀
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(
                new StatViewServlet(),
                "/druid/*"
        );
        //创建初始化参数Map
        Map<String, String> initParams = new HashMap<>(10);
        //设置登录名称密码网址过滤条件
        initParams.put("loginUsername", "druid");
        initParams.put("loginPassword", "tq1j8BZ510opGnjZTKrzV3Che2BLTc0F");
        //默认就是允许所有访问
        initParams.put("allow", "");
//        initParams.put("deny", "");
        bean.setInitParameters(initParams);
        return bean;
    }

    /**
     * 2、配置一个web监控的filter
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean<WebStatFilter> webStatFilter() {
        //创建注册过滤对象
        FilterRegistrationBean<WebStatFilter> bean = new FilterRegistrationBean<>();
        //设置过滤器
        bean.setFilter(new WebStatFilter());
        //创建初始化参数Map
        Map<String, String> initParams = new HashMap<>(10);
        //增加过滤排除条件
        initParams.put("exclusions", "*.js,*.css,/druid/*");
        //设置参数
        bean.setInitParameters(initParams);
        //设置过滤范围
        bean.setUrlPatterns(Collections.singletonList("/*"));
        return bean;
    }


}